package Screen
{
	import Ecuations.DataObject;
	
	import flash.display.Sprite;
	import flash.text.TextField;
	
	public class GraphicScreen extends Sprite
	{
		private var _dataList:Array;
		private var sectList:Array;
		
		private var _totalDistance:Number = 0;
		private var lastValueX:Number = 0;
		private var lastValueY:Number = 0;
		
		
		//layer donde se dibujan los ejes
		private var axis:Sprite;
		//layer donde se dibuja el grafico
		private var graphicLayer:Sprite;
		
		private var _scaleFactorPosition:Number = 30;
		private var _scaleFactorTime:Number = 2500;	
		
		//EL SEGUNDO PARAMETRO SERIA UNA LISTA DE TODOS LOS DATA OBJECTS DEL RECORRIDO
		public function GraphicScreen(sectorList:Array, dataObjsList:Array=null)
		{
			super();
			
			graphicLayer = new Sprite();
			graphicLayer.y = 200;
			
			this.addChild(graphicLayer);
			
			_dataList = dataObjsList;
			sectList = sectorList;
			
			
		}
		
		private function calculateXAxisLength() : void
		{
			for (var i:int = 0; i < sectList.length; i++)
			{
				_totalDistance += (sectList[i].destPos / 1000)*_scaleFactorPosition; //convierto de m a km
				createStationMarker( _totalDistance, 1, _scaleFactorPosition );
			}
			
		}
		
		private function calculateAxisLengthBasedOnTime() : void
		{
			for (var i:int = 0; i < _dataList.length; i++)
			{
				_totalDistance += (_dataList[i].time/3600)*_scaleFactorTime;
				createStationMarker( int(_totalDistance), 1 , _scaleFactorTime);
			}
			
			this.scaleX = 2;
			this.scaleY = 2;
			
		}
		
		private function createStationMarker(pos:Number, textScale:Number, numberScale:Number) : void
		{
			var marker:Sprite = new Sprite();
			var markerValue:TextField = new TextField();
			
			with(marker.graphics)
			{
				lineStyle(0.5, 0x000000);
				moveTo(pos, 200);
				lineTo(pos, 210);
			}
			
			markerValue.text = String( pos/numberScale );
			
			markerValue.y = 207;
			markerValue.x = pos - 7;
			
			markerValue.scaleX = textScale;
			markerValue.scaleY = textScale;
			markerValue.mouseEnabled = false;
			
			this.addChild( markerValue );
			this.addChild( marker );
		}
		
		public function scaleMarkers(value:Number) : void
		{
			for(var i:int = 0; i < this.numChildren; i++)
			{
				var child:* = this.getChildAt(i);
				if (this.getChildAt(i) is TextField)
				{
					child.scaleX += value;
					child.scaleY += value;
				}
					
			}
		}
		
		private function drawAxis() : void
		{
			axis = new Sprite();
			
			with(axis.graphics)
			{
				lineStyle(0.5, 0x000000);
				moveTo(0, 200);
				lineTo(0, 0);
				moveTo(0, 200);
				lineTo( _totalDistance, 200); 
			}
			this.addChild(axis);
		}
		
		
		//===================================================
		//		TIPOS DE GRAFICOS (si, los metodos estan en castellano :-P )
		//		ATENCION: CODIGO DUPLICADO SE AVECINA! 
		//===================================================
		public function velocidadEnFuncionDelLaProgresiva() : void
		{
			calculateXAxisLength();
			drawAxis();
			
			graphicLayer.graphics.lineStyle(0.5, 0x000000);
			graphicLayer.graphics.moveTo(lastValueX, lastValueY);
			
			for(var i:int = 0; i < _dataList.length; i++)
			{
				graphicLayer.graphics.lineTo( (_dataList[i].deltaX / 1000)*_scaleFactorPosition, _dataList[i].velocity*3.6 );
				lastValueX = (_dataList[i].deltaX/1000)*_scaleFactorPosition;
				lastValueY = _dataList[i].velocity*3.6;
			}
			graphicLayer.scaleY = -1;
		}
		
		public function aceleracionEnFuncionDelTiempo() : void
		{
			calculateAxisLengthBasedOnTime();
			drawAxis();
			
			graphicLayer.graphics.lineStyle(0.5, 0x000000);
			
			
			for(var i:int = 0; i < _dataList.length; i++)
			{
				graphicLayer.graphics.moveTo(lastValueX, _dataList[i].accel);
				
				graphicLayer.graphics.lineTo( lastValueX + (_dataList[i].time/3600)*_scaleFactorTime , _dataList[i].accel );
				lastValueX += (_dataList[i].time/3600)*_scaleFactorTime;
				lastValueY = _dataList[i].accel;
			}
			graphicLayer.scaleY = -1;
		}
		
		public function velocidadEnFuncionDelTiempo() : void
		{
			calculateAxisLengthBasedOnTime();
			drawAxis();
			
			graphicLayer.graphics.lineStyle(0.5, 0x000000);
			graphicLayer.graphics.moveTo(lastValueX, lastValueY);
			
			for(var i:int = 0; i < _dataList.length; i++)
			{
				lastValueX += (_dataList[i].time/3600)*_scaleFactorTime;
				lastValueY = _dataList[i].velocity*3.6;
				graphicLayer.graphics.lineTo( lastValueX, lastValueY );
				
			}
			graphicLayer.scaleY = -1;
		}
		
		public function posicionEnFuncionDelTiempo() : void
		{
			calculateAxisLengthBasedOnTime();
			drawAxis();
			
			graphicLayer.graphics.lineStyle(0.5, 0x000000);
			graphicLayer.graphics.moveTo(lastValueX, lastValueY);
			
			for(var i:int = 0; i < _dataList.length; i++)
			{
				lastValueX += (_dataList[i].time/3600)*_scaleFactorTime;
				lastValueY = (_dataList[i].deltaX/1000)*_scaleFactorPosition;
				graphicLayer.graphics.lineTo( lastValueX, lastValueY );
				
			}
			graphicLayer.scaleY = -1;
		}
	}
}